
#include "pywinpcapmodule.h"

extern PyObject * PyExc_PyWinPCapError;
extern PyObject * PyExc_PyWinPCapTimeoutError;

static void
pywinpcap_device_dealloc(pywinpcap_device_object * self)
{
	Py_XDECREF(self->m_device);
	Py_XDECREF(self->m_name);
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject * pywinpcap_device_new(	PyTypeObject * type,
										PyObject *args,
										PyObject *kwds)
{
    pywinpcap_device_object * self = NULL;

    self = (pywinpcap_device_object *)type->tp_alloc(type, 0);
    if (NULL == self)
	{
		return NULL;
	}

	self->m_device = (PyStringObject *)PyString_FromString("");
    if (NULL == self->m_device)
    {
		Py_DECREF(self);
        return NULL;
    }
        
    self->m_name = (PyStringObject *)PyString_FromString("");
    if (self->m_name == NULL)
    {
		Py_DECREF(self);
        return NULL;
    }

    return (PyObject *)self;
}

static int pywinpcap_device_init(	pywinpcap_device_object * self,
									PyObject * args,
									PyObject * kwds)
{
    PyStringObject * device = NULL;
	PyStringObject * name = NULL;
	PyStringObject * temp = NULL;

	if (!PyArg_ParseTuple(args, "OO", &device, &name))
	{
		return -1;
	}

	if (!PyString_Check(device) || !PyString_Check(name))
	{
		return -1;
	}

    temp = self->m_device;
    Py_INCREF(device);
    self->m_device = device;
    Py_XDECREF(temp);
    
    temp = self->m_name;
    Py_INCREF(name);
    self->m_name = name;
    Py_XDECREF(temp);

    return 0;
}

static PyObject * pywinpcap_device_str(pywinpcap_device_object * self)
{
	return PyString_FromFormat("<Device '%s' ('%s')>", PyString_AS_STRING(self->m_device), PyString_AS_STRING(self->m_name));
}

static PyMemberDef pywinpcap_device_members[] = {
    {"device", T_OBJECT_EX, offsetof(pywinpcap_device_object, m_device), 0, "device name"},
    {"name", T_OBJECT_EX, offsetof(pywinpcap_device_object, m_name), 0, "device human readable"},
    {NULL}  /* Sentinel */
};

static PyMethodDef pywinpcap_device_methods[] = {
    {NULL}  /* Sentinel */
};

PyTypeObject pywinpcap_device_type = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "pywinpcap.Device",             /*tp_name*/
    sizeof(pywinpcap_device_object), /*tp_basicsize*/
    0,                         /*tp_itemsize*/
	(destructor)pywinpcap_device_dealloc,  /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    (reprfunc)pywinpcap_device_str, /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    (reprfunc)pywinpcap_device_str,  /*tp_str*/	
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
    "Device object",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
	pywinpcap_device_methods,  /* tp_methods */
    pywinpcap_device_members,  /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
	(initproc)pywinpcap_device_init,	/* tp_init */
    0,                         /* tp_alloc */
	pywinpcap_device_new,      /* tp_new */
};
